package com.sky.controller.admin;



import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageDTO;
import com.sky.dto.TagDTO;
import com.sky.dto.TagPageDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.TagService;
import com.sky.vo.CategoryVO;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RestController
@RequestMapping("/admin/tag")
@Tag(name = "标签相关接口")
public class TagController {

    private final TagService tagService;


    @Autowired
    public TagController(TagService tagService) {
        this.tagService = tagService;
    }

    /**
     * 分页查询获取所有的标签
     * @param tagPageDTO
     * @return
     */
    @GetMapping("/list")
    @Operation(summary = "分页查询获取所有的标签")
    public Result<PageResult> list(TagPageDTO tagPageDTO) {
        log.info("分页查询获取所有的标签：{}", tagPageDTO);
        PageResult pageResult =  tagService.list(tagPageDTO);
        return Result.success(pageResult);
    }

    /**
     * 新建标签
     */
    @PostMapping("/create")
    @Operation(summary = "新建标签")
    public Result create(@RequestBody TagDTO tagDTO) {
        log.info("新建标签：{}", tagDTO);
        tagService.create(tagDTO);
        return Result.success();
    }

    /**
     * 根据名称删除标签
     */
    @DeleteMapping("/delete")
    @Operation(summary = "删除标签")
    public Result delete(@RequestParam Long id) {
        log.info("删除标签：{}", id);
        tagService.delete(id);
        return Result.success();
    }

    /**
     * 根据id查询标签
     * @param id
     * @return
     */
    @GetMapping("/getById")
    @Operation(summary = "根据id查询标签")
    public Result<CategoryVO> getById(Long id) {
        log.info("根据id查询标签：{}", id);
        CategoryVO categoryVO = tagService.getById(id);
        return Result.success(categoryVO);
    }

    /**
     * 修改标签
     * @param tagDTO
     * @return
     */
    @PutMapping("/update")
    @Operation(summary = "修改标签")
    public Result update(@RequestBody TagDTO tagDTO) {
        log.info("修改标签：{}", tagDTO);
        tagService.update(tagDTO);
        return Result.success();
    }

    /**
     * 统计标签数量
     * @return
     */
    @GetMapping("/count")
    @Operation(summary = "统计标签数量")
    public Result<Long> countTag() {
        log.info("统计标签数量");
        Long count = tagService.countTag();
        return Result.success(count);
    }


}
